package databasetest;

import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.PreparedStatement;

import javax.swing.JInternalFrame;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;

public class Insert extends JInternalFrame {
	Connection conn; // 전달받을 코넥션
	DataBaseTest main;
	private JTextField code;
	private JTextField ea;
	private JLabel lblNewLabel;
	private JLabel lblNewLabel_1;
	private JButton btnNewButton;
	private JLabel result;

	public Insert(Connection conn,DataBaseTest d) {
		super("입력",true,true,true,true);
		addInternalFrameListener(new InternalFrameAdapter() {
			@Override
			public void internalFrameClosing(InternalFrameEvent arg0) {
				code.setText("");
				ea.setText("");
				
			}
		});
		this.conn = conn;
		this.main = d;
		setBounds(400, 400, 450, 300);
		getContentPane().setLayout(null);
		getContentPane().add(getCode());
		getContentPane().add(getEa());
		getContentPane().add(getLblNewLabel());
		getContentPane().add(getLblNewLabel_1());
		getContentPane().add(getBtnNewButton());
		getContentPane().add(getResult());

	}
	private JTextField getCode() {
		if (code == null) {
			code = new JTextField();
			code.setBounds(178, 59, 116, 21);
			code.setColumns(10);
		}
		return code;
	}
	private JTextField getEa() {
		if (ea == null) {
			ea = new JTextField();
			ea.setBounds(178, 118, 116, 21);
			ea.setColumns(10);
		}
		return ea;
	}
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("코드");
			lblNewLabel.setBounds(109, 62, 57, 15);
		}
		return lblNewLabel;
	}
	private JLabel getLblNewLabel_1() {
		if (lblNewLabel_1 == null) {
			lblNewLabel_1 = new JLabel("수량");
			lblNewLabel_1.setBounds(109, 121, 57, 15);
		}
		return lblNewLabel_1;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("저장");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					try{
						String c = code.getText(); //코드 입력상자에 값
						int e = Integer.parseInt(ea.getText()); //수량 입력상자에 값

						String sql = "insert into protbl(serial,code,ea) values(ptbl_serial.nextval, ?,?)";
						// 삽입하는 순서는 시리얼 , 코드 , 수량 이며 serial 값은 시퀀스를 사용해서 입력함.
						PreparedStatement pstmt = conn.prepareStatement(sql);
						pstmt.setString(1, c);//첫번째 물음표에 코드값
						pstmt.setInt(2, e); //두번째 물음표에 수량값(수량은 인트형태기 때문에)

						pstmt.executeUpdate();//입력,수정,삭제

						result.setText("code 값 : "+code.getText()+"  수량 값   "+ea.getText()+"  입력되었습니다.  ");
						code.setText("");
						ea.setText("");

						if(main.select !=null) main.select.search();
                          

					}catch(Exception ex){
						ex.printStackTrace();
					}


				}
			});
			btnNewButton.setBounds(178, 178, 116, 23);
		}
		return btnNewButton;
	}
	private JLabel getResult() {
		if (result == null) {
			result = new JLabel("New label");
			result.setBounds(12, 226, 410, 15);
		}
		return result;
	}
}
